package main

import (
	"context"
	"fmt"
	"gitee.com/hangzhou_huitian/ht-basic/routine"
	"gitee.com/hangzhou_huitian/ht-basic/swcron"
	"github.com/gin-gonic/gin"
	"hisim/busi"
	"hisim/controller"
	"hisim/global"
	"sync"
)

//TIP <p>To run your code, right-click the code and select <b>Run</b>.</p> <p>Alternatively, click
// the <icon src="AllIcons.Actions.Execute"/> icon in the gutter and select the <b>Run</b> menu item from here.</p>

type CFG = global.XConfig

func init() {
	ii := routine.InitInfo[CFG]{
		ConfigFileName: "hisim_conf.toml",
		BaseInfo: func(t *CFG) *routine.BaseInfo {
			return &routine.BaseInfo{
				DBServer:   t.DBServer,
				DBName:     "hisim",
				InitBasic:  true,
				InitConfig: false,
				OpenLog:    true,
			}
		},
		UsePool: false,
		ScheduleInfo: func(t *CFG) (c *swcron.Config, job func(jobName string, param ...interface{})) {
			return &t.Config, busi.ScheduleWork
		},
		CustomInit: func(ir *routine.InitResult[CFG]) error {
			global.Conf = ir.T
			global.DB = ir.DB
			busi.PrevLoad(ir.DB)
			return nil
		},
	}

	if err := routine.InitEntry(&ii); err != nil {
		fmt.Printf("init entry failed, err:%v\n", err)
	}
}

func main() {
	wg := &sync.WaitGroup{}
	mi := routine.MainInfo{
		HttpInfo: func() (httpPort uint16, ginReg func(ctx *gin.Engine), openLog bool) {
			conf := global.Conf
			return conf.HttpPort, controller.HttpEntrance, conf.OpenHttpLog
		},
		CustomMain: func(ctx context.Context) error {
			wg.Add(1)
			go func() {
				wg.Done()
			}()
			return prepareMain(ctx)
		},
		AfterMain: func() error {
			wg.Wait()
			return nil
		},
	}

	if err := routine.MainEntry(&mi); err != nil {
		fmt.Printf("main entry failed, err:%v\n", err)
	}
}

func prepareMain(ctx context.Context) error {

	return nil
}
